package com.wzdigit.wms.wms.controller.TransferWareHouse;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.transfer.SubmitTransferRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.transfer.TransferHeaderPageResponse;
import com.wzdigit.wms.basic.client.response.transfer.TransferHeaderResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.transfer.TurnWshReceivedService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 转仓接收_PDA 前端控制器
 * </p>
 *
 * @author
 * @since 2021-01-12
 */
@Slf4j
@RestController
@RequestMapping(value = WmsWareConstant.API_VERSION)
@Api(tags = "转仓接收_PDA")
public class TurnWshReceivedController {

    public static final String REQUEST_MODEL = "/turnWshReceived";

    @Autowired
    private TurnWshReceivedService turnWshReceivedService;
    
	/**
	 * 转仓单列表
	 * @param transferNo
	 * @param pager
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "转仓单列表",notes = "转仓单列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchTransfer")
    public PagerInfo<List<TransferHeaderPageResponse>> searchTransfer(
    		@ApiParam(value = "转仓单号", required = false) 
    		@RequestParam(required = false) String transferNo, 
    		Pager<?> pager, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return turnWshReceivedService.searchTransfer(transferNo, pager, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 转仓单详情
	 * @param transferNo
	 * @param goodsSn
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "转仓单详情",notes = "转仓单详情")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getTransfer")
    public TransferHeaderResponse getTransfer(
    		@ApiParam(value = "转仓单号", required = false) 
    		@RequestParam(required = false) String transferNo, 
    		@ApiParam(value = "SN", required = false) 
    		@RequestParam(required = false) String goodsSn,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return turnWshReceivedService.getTransfer(goodsSn, transferNo, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
    /**
     * 转仓单点收
     * @param transferHeaderId
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "转仓单点收",notes = "转仓单点收")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/submitTransfer")
    public Result<?> submitTransfer(
    		@RequestBody @Valid SubmitTransferRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
        try {
        	ValidatorUtil.validator(bindingResult);
            return turnWshReceivedService.submitTransfer(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}
